package com.leyou.order.controller;

import com.leyou.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping
public class PayController {

    @Autowired
    private OrderService orderService;

    /**
     * @ResponseBody注解的作用，是将对象依据标准，转成json或者xml
     * @RequestBody注解将json或者xml转成对象的前提条件是，
     * 数据必须是utf-8格式，必须有转json或者xml的相关jar包，必须是post请求。
     * produces指定返回值数据的类型
     * @return
     */
    @PostMapping(value = "/pay/wx/notify", produces = "application/xml")
    public ResponseEntity<Map<String, String>> wxNotify(@RequestBody Map<String, String> notifyMap){
        log.info("【微信回调通知】开始！");
        //判断微信通知是否合法
        orderService.wxNotify(notifyMap);
        Map<String, String> returnMap = new HashMap<>();
        returnMap.put("return_code", "SUCCESS");
        returnMap.put("return_msg", "OK");
        log.info("【微信回调通知】成功！");
        return ResponseEntity.ok(returnMap);
    }

}
